package jmovie;

import java.awt.Component;

import javax.swing.DefaultCellEditor;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JTable;
import javax.swing.table.AbstractTableModel;
import javax.swing.table.TableCellRenderer;

class MyTable extends AbstractTableModel {
	private static final long serialVersionUID = 1L;
	
	private String[] columnNames;
    private Object[][] data;

	public MyTable(Object[][] data, String[] columnNames) {
		this.data = data;
		this.columnNames = columnNames;
	}
    
    public int getColumnCount() {
        return columnNames.length;
    }

    public int getRowCount() {
        return data.length;
    }
    
    public String getColumnName(int col) {
        return columnNames[col];
    }

    public Object getValueAt(int row, int col) {
        return data[row][col];
    }

    /*
     * JTable uses this method to determine the default renderer/
     * editor for each cell.  If we didn't implement this method,
     * then the last column would contain text ("true"/"false"),
     * rather than a check box.
     */
    public Class<?> getColumnClass(int c) {
        return getValueAt(0, c).getClass();
    }

    public boolean isCellEditable(int row, int col) {
    	if (getColumnName(col).equals("Scheda"))
    		return true;
        return false;
    }

    /*
     * Don't need to implement this method unless your table's
     * data can change.
     */
    public void setValueAt(Object value, int row, int col) {
        data[row][col] = value;
        fireTableCellUpdated(row, col);
    }
}

class ButtonRenderer extends JButton implements TableCellRenderer {
	private static final long serialVersionUID = 1L;

	public ButtonRenderer() {
		setOpaque(true);
	}

	public Component getTableCellRendererComponent(JTable table, Object value,
			boolean isSelected, boolean hasFocus, int row, int column) {

		setText((value == null) ? "" : value.toString());
		return this;
	}
}

class ButtonEditor extends DefaultCellEditor {
	private static final long serialVersionUID = 1L;
	private String buttonLabel;
	private JButton button;
	
	public ButtonEditor(JCheckBox checkBox, JButton button) {
		super(checkBox);
		this.button = button;
	}

	public Component getTableCellEditorComponent(JTable table, Object value,
			boolean isSelected, int row, int column) {

		buttonLabel = (value == null) ? "" : value.toString();
		button.setText(buttonLabel);
		return button;
	}

	public Object getCellEditorValue() {
		return new String(buttonLabel);
	}
}

